

-- 获取弹道发射x
function ac.unit.__index:get_launch_x()
    return self.launchX or self:get_slk('launchX', 0)
end

-- 获取弹道发射y
function ac.unit.__index:get_launch_y()
    return self.launchY or self:get_slk('launchY', 0)
end

-- 获取弹道发射z
function ac.unit.__index:get_launch_z()
    return self.launchZ or self:get_slk('launchZ', 0)
end

--模拟弹道
local buff = ac.buff['弹道设置']
--buff.model1 = [[Abilities\Weapons\Arrow\ArrowMissile.mdl]]
--buff.model1 = [[Abilities\Spells\Items\WandOfNeutralization\NeutralizationMissile.mdl]]
buff.cover_type = 1
buff.cover_max = 1
buff.keep = true

buff.pulse = 1


function buff:on_add()
    local hero = self.owner
    self:gc(hero:event '单位-攻击出手'(function(_,source,target)
        local start = source:get_launch_point()
        local count1 = math.floor(hero:get('多重数量'))
        local count2 = math.floor(hero:get('弹射次数'))
        local missile_art = self.model1 or hero:get_slk('Missileart')
        local missile_speed = self.speed or hero['弹道速度'] or tonumber(hero:get_slk('Missilespeed'))
        local missile_arc = self.arc or hero['弹道弧度'] or tonumber(hero:get_slk('Missilearc')) or 0

        local list = {}
        if count1>0 then
            local area = math.min(hero:get('攻击距离')+100,1200)
            list = ac.selector('unit'):range(hero,area):enemy(hero):is_not(target):near(target):get(count1)
        end
        list[#list + 1] = target
        for _,unit in ipairs(list) do
            if unit==target then
                local temp = {
                    damage_count = 0,
                    damage_max = count2,
                    target = target,
                }
                self:catapult(temp,start,unit)
            else
                start:launch_missile(unit,missile_art,missile_speed,missile_arc,function(mvr)
                    hero:create_damage(unit, hero:get('攻击') * hero:get('多重伤害')*0.01)
                end)
            end
        end
        
        source:notify('单位-攻击出手后',source,target)

        return true
    end))
end

--[弹射和多重处理]
function buff:catapult(temp,start,dest,params)
    local hero = self.owner
    local missile_art = self.model1 or hero:get_slk('Missileart')
    local missile_speed = self.speed or hero['弹道速度'] or tonumber(hero:get_slk('Missilespeed'))
    local missile_arc = self.arc or hero['弹道弧度'] or tonumber(hero:get_slk('Missilearc')) or 0
    temp[dest.handle] = true
    missile_speed = missile_speed~=0 and missile_speed or 1200
    if temp.damage_count>0 then
        missile_arc = 0
    end
    start:launch_missile(dest,missile_art,missile_speed,missile_arc,function(mvr)
        local atk = hero:get('攻击')
        if temp.target==dest then
            hero:create_damage(dest, atk, '普攻')
        else
            hero:create_damage(dest, atk * hero:get('弹射伤害')*0.01)
        end
        if temp.damage_count < temp.damage_max then
            temp.damage_count = temp.damage_count + 1
            local point = dest:get_impact_point()
            local target = ac.selector('unit'):range(dest,600):is_not(dest):enemy(hero):sorter(function(a,b)
                if temp[a.handle] and temp[b.handle]==nil then
                    return false
                end
                if temp[a.handle]==nil and temp[b.handle] then
                    return true
                end
                local r1 = a:get_point() * point
                local r2 = b:get_point() * point
                return r1 < r2
            end):first()
            if target then
                mvr.need_remove_mover = false
                self:catapult(temp,point,target,{mover = mvr.mover})
            else
                mvr.need_remove_mover = true
            end
        end
    end,params)
end


ac.game:event '玩家-选择英雄完毕'(function(_,player,hero)
    hero:add_buff '弹道设置'{}
end)